//
//  SXWelcomeController.swift
//  微博09项目
//
//  Created by mac on 15/9/7.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage
class SXWelcomeController: UIViewController {

    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        iconImage.sd_setImageWithURL(UserAccountViewModel.shareUserAccount.avatarUrl)
        setupUI()
    }
    override func viewDidAppear(animated: Bool) {
        //计算目标的约束数值
        let height = -(UIScreen.mainScreen().bounds.height + iconBottomCons!.constant)
        //修改约束值
        iconBottomCons?.constant = height
        
        welcomeLabel.alpha = 0
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        //TODO:-发送切换控制器的通知
                        NSNotificationCenter.defaultCenter().postNotificationName(SXSwitchRootViewControllerNotification, object: nil)

                })
        }

    }
    
    private func setupUI(){
        view.addSubview(backImage)
        view.addSubview(iconImage)
        view.addSubview(welcomeLabel)
        //添加背景图片的约束
        backImage.ff_Fill(view)
        
        //添加头像的约束
        
       let icons = iconImage.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -200))
      
        self.iconBottomCons = iconImage.ff_Constraint(icons, attribute: NSLayoutAttribute.Bottom)
        
        //添加欢迎文本的约束
     welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconImage, size: nil, offset: CGPoint(x: 0, y: 16))
        
    }
    
    private lazy var backImage:UIImageView = {
        let imageView = UIImageView()
        
        imageView.image = UIImage(named: "ad_background")
        
        return imageView
    }()
    private lazy var welcomeLabel:UILabel = {
      var label = UILabel()
        
        label.text = "欢迎归来"
        label.sizeToFit()
        label.textAlignment = NSTextAlignment.Center
       
        return label
    }()
    private lazy var iconImage:UIImageView = {
        let iconImage = UIImageView()
      
        iconImage.image = UIImage(named: "avatar_default_big")
        iconImage.layer.cornerRadius = 45
        iconImage.layer.masksToBounds = true
        iconImage.sizeToFit()
        
        return iconImage
        
    }()

   }
